
/**
 * 根据当前页和页数 获取数据库中 limit 和 offset的值
 * @author guzhanghua
 * @param {*} currentPage   当前页
 * @param {*} pageNumber    每页页数
 * @returns { currentPage , pageNumber , offset : 跳过的实例个数 , limit : 查询的实例个数}
 */
function formatPage(currentPage, pageNumber) {
	let formatNumber = (number, defaultNumber) => {
		number = Number(number);
		if (isNaN(number) || !/(^[1-9]\d*$)/.test(number)) {
			return defaultNumber;
		} else {
			return number;
		}
	};
	currentPage = formatNumber(currentPage, 1);
	pageNumber = formatNumber(pageNumber, 10);
	// 定义跳过的实例个数
	let offset = pageNumber * (currentPage - 1);
	// 定义查询的实例个数
	let limit = pageNumber;
	return {
		currentPage,
		pageNumber,
		offset,
		limit
	};
}

exports.formatPage = formatPage;